package com.fare.taluo.application.impl.fetl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.fare.taluo.application.fetl.DoublePlaneInfoApplication;
import com.fare.taluo.fetl.domain.entity.DoublePlanInfoJsonEntity;
import com.fare.taluo.fetl.domain.entity.DoublePlaneInfoEntity;
import com.fare.taluo.util.JsonUtil;
import com.fare.taluo.util.PlaneInfoVOToPlaneInfo;
import com.fare.taluo.vo.core.PlaneInfo;
import com.fare.taluo.vo.doub.CabinRelationVO;
import com.fare.taluo.vo.doub.DoublePlanInfoJsonVO;
import com.fare.taluo.vo.doub.DoublePlaneInfoVO;
import com.fare.taluo.vo.doub.ReturnAgentVO;
import com.fare.taluo.vo.doub.ReturnCabinVO;
import com.fare.taluo.vo.doub.ReturnDoublePlaneInfoVO;
import com.fare.taluo.vo.doub.ReturnStopOverVO;
import com.fare.taluo.vo.doub.ReturnTransitVO;
import com.fare.taluo.vo.single.AgentVO;
import com.fare.taluo.vo.single.CabinVO;
import com.fare.taluo.vo.single.StopOverVO;
import com.fare.taluo.vo.single.TransitVO;

@Scope("singleton")
@Service("doublePlaneInfoApplication")
public class DoublePlaneInfoApplicationImpl implements
		DoublePlaneInfoApplication {

	@Override
	public List<DoublePlaneInfoVO> findPlaneInfoProperty(String hbaseKey,
			String fromCity, String toCity, String flightBatchDate,
			String returnFlightDate, String flightNo) {
		List<DoublePlaneInfoVO> listVo =new ArrayList<DoublePlaneInfoVO>();
		DoublePlaneInfoEntity entity =new DoublePlaneInfoEntity();
		List<DoublePlaneInfoEntity> listEntity = entity.findPlaneInfoProperty(
				hbaseKey, fromCity, toCity, flightBatchDate, returnFlightDate,
				flightNo);
		if(null!=listEntity && listEntity.size()>0){
			for(DoublePlaneInfoEntity en : listEntity){
				DoublePlaneInfoVO vo=this.planeInfoEntityToVo(en);
				if(null!=vo && null!=vo.getFlightNo()){
					listVo.add(vo);
				}
			}
		}
		return listVo;
	}

	private DoublePlaneInfoVO planeInfoEntityToVo(DoublePlaneInfoEntity entity) {
		
		DoublePlaneInfoVO vo =new DoublePlaneInfoVO();
		vo.setActuallyFlightNo(entity.getActuallyFlightNo());
		vo.setAgentName(entity.getAgentName());
		vo.setAreaCode(entity.getAreaCode());
		vo.setAreaName(entity.getAgentName());
		vo.setAttachHbaseKey(entity.getAttachHbaseKey());
		vo.setCarrierFullName(entity.getCarrierFullName());
		vo.setCarrierKey(entity.getCarrierKey());
		vo.setCarrierName(entity.getCarrierName());
		vo.setCreateTime(entity.getCreateTime());
		vo.setEndTime(entity.getEndTime());
		vo.setFlightDate(entity.getFlightDate());
		vo.setFlightNo(entity.getFlightNo());
		vo.setFlightType(entity.getFlightType());
		vo.setFromCity(entity.getFromCity());
		vo.setFromCityName(entity.getFromCityName());
		vo.setGrabChannelId(entity.getGrabChannelId());
		vo.setGrabChannelName(entity.getGrabChannelName());
		vo.setGrabDate(entity.getGrabDate());
		vo.setId(entity.getId());
		vo.setSourceRowkey(entity.getSourceRowkey());
		vo.setStartTime(entity.getStartTime());
		vo.setToCity(entity.getToCity());
		vo.setToCityName(entity.getToCityName());
		vo.setTotalHighestPrice(entity.getTotalHighestPrice());
		vo.setTotalLowestPrice(entity.getTotalLowestPrice());
		vo.setReturnFlightDate(entity.getReturnFlightDate());
		if(null!=entity.getDoublePlanInfoJsonEntity()){
			vo.setDoublePlanInfoJsonVO(this.planJsonEntityToVO(entity.getDoublePlanInfoJsonEntity()));
			if(null!=entity.getDoublePlanInfoJsonEntity().getAgentsJson()){
				Set<AgentVO> setAgents =new HashSet<AgentVO>(JsonUtil.getDTOList(entity.getDoublePlanInfoJsonEntity().getAgentsJson(), AgentVO.class));
				  vo.setAgents(setAgents);
			}
			if(null!=entity.getDoublePlanInfoJsonEntity().getCabinsJson()){
				Set<CabinVO> setCabins =new HashSet<CabinVO>(JsonUtil.getDTOList(entity.getDoublePlanInfoJsonEntity().getCabinsJson(), CabinVO.class));
				vo.setCabins(setCabins);
			}
			if(null!=entity.getDoublePlanInfoJsonEntity().getCabinRelationsJson()){
				Set<CabinRelationVO> cabinRelations= new HashSet<CabinRelationVO>(JsonUtil.getDTOList(entity.getDoublePlanInfoJsonEntity().getCabinRelationsJson(), CabinRelationVO.class));
				vo.setCabinRelations(cabinRelations);
			}
			if(null!=entity.getDoublePlanInfoJsonEntity().getStopOversJson()){
				Set<StopOverVO> setStopOver =new HashSet<StopOverVO>(JsonUtil.getDTOList(entity.getDoublePlanInfoJsonEntity().getStopOversJson(), StopOverVO.class));
				vo.setStopOvers(setStopOver);
			}
			if(null!=entity.getDoublePlanInfoJsonEntity().getTransitsJson()){
				Set<TransitVO> setTransit =new HashSet<TransitVO>(JsonUtil.getDTOList(entity.getDoublePlanInfoJsonEntity().getTransitsJson(), TransitVO.class));
				vo.setTransits(setTransit);
			}
			if(null!=entity.getDoublePlanInfoJsonEntity().getReturnPlaneInfosJson()){
				Map <String,Class> mymap = new HashMap<String,Class>();
				mymap.put("returnTransits", ReturnTransitVO.class);
				mymap.put("returnCabins", ReturnCabinVO.class);
				mymap.put("returnAgents", ReturnAgentVO.class);
				mymap.put("returnStopOvers", ReturnStopOverVO.class);
				Set<ReturnDoublePlaneInfoVO> returnDoublePlaneInfos=new HashSet<ReturnDoublePlaneInfoVO>(JsonUtil.getDTOList(entity.getDoublePlanInfoJsonEntity().getReturnPlaneInfosJson(), ReturnDoublePlaneInfoVO.class,mymap));
				vo.setReturnPlaneInfos(returnDoublePlaneInfos);
			}
		}
		
		return vo;
	}

	private DoublePlanInfoJsonVO planJsonEntityToVO(
			DoublePlanInfoJsonEntity entity) {
		DoublePlanInfoJsonVO vo =new DoublePlanInfoJsonVO();
		vo.setId(entity.getId());
		vo.setAgentsJson(entity.getAgentsJson());
		vo.setCabinsJson(entity.getCabinsJson());
		vo.setStopOversJson(entity.getStopOversJson());
		vo.setTransitsJson(entity.getTransitsJson());
		vo.setCabinRelationsJson(entity.getCabinRelationsJson());
		vo.setReturnPlaneInfosJson(entity.getReturnPlaneInfosJson());
		return vo;
	}

	@Override
	public List<PlaneInfo> findPlaneInfo(String hbaseKey, String fromCity,
			String toCity, String flightBatchDate, String returnFlightDate,
			String flightNo) {
		List<DoublePlaneInfoVO> list = this.findPlaneInfoProperty(hbaseKey,
				fromCity, toCity, flightBatchDate, returnFlightDate, flightNo);
		List<PlaneInfo> listPlaneInfo =new ArrayList<PlaneInfo>();
		if(null!=list && list.size()>0){
			for(DoublePlaneInfoVO vo :list){
				PlaneInfo planeInfo =PlaneInfoVOToPlaneInfo.doubleToPlaneInfo(vo);
				listPlaneInfo.add(planeInfo);
			}
		}
		return listPlaneInfo;
	}
	@Override
	public List<PlaneInfo>  queryFreightPrice(String hbaseKey,String carrierKey,
			String fromCity, String toCity, String flightBatchDate,
			String returnFlightDate){
		List<DoublePlaneInfoVO> list = this.findPlaneInfoProperty(hbaseKey,
				fromCity, toCity, flightBatchDate, returnFlightDate, null);
		List<PlaneInfo> listPlaneInfo =new ArrayList<PlaneInfo>();
		if(null!=list && list.size()>0){
			for(DoublePlaneInfoVO vo :list){
				if(carrierKey.equalsIgnoreCase(vo.getCarrierKey())){
					PlaneInfo planeInfo =PlaneInfoVOToPlaneInfo.doubleToPlaneInfo(vo);
					listPlaneInfo.add(planeInfo);
				}
			}
		}
		return listPlaneInfo;
	}
}
